package com.easyweb.common;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;

import com.easyweb.model.DiskInfo;
import com.easyweb.model.MemoryInfo;
import com.easyweb.model.SystemInfo;
/**
 * 系统信息工具类
 * @author zhangz
 *
 */
public class SystemUtils {
	/**
	 * 获取磁盘信息
	 * @param sigar
	 * @return
	 * @throws SigarException
	 */
	public static List<DiskInfo> getDiskInfo(Sigar sigar) throws SigarException{
		List<DiskInfo> list = new ArrayList<DiskInfo>();
		FileSystem fslist[] = sigar.getFileSystemList();
		for (int i = 0; i < fslist.length; i++) {
			DiskInfo disk = new DiskInfo();
			FileSystem fs = fslist[i];
			// 分区的盘符名称
			disk.setDevName(fs.getDevName());
			// 分区的盘符名称
			disk.setDirName(fs.getDirName());
			// 文件系统类型，比如 FAT32、NTFS
			disk.setSysTypeName(fs.getSysTypeName());
			// 文件系统类型名，比如本地硬盘、光驱、网络文件系统等
			disk.setTypeName(fs.getTypeName());
			disk.setType(fs.getType());
			// 文件系统类型
			FileSystemUsage usage = null;
			try {
				usage = sigar.getFileSystemUsage(fs.getDirName());
			} catch (SigarException e) {
				if (fs.getType() == 2)
					throw e;
				continue;
			}
			switch (fs.getType()) {
				case 0: // TYPE_UNKNOWN ：未知
					break;
				case 1: // TYPE_NONE
					break;
				case 2: // TYPE_LOCAL_DISK : 本地硬盘
					// 文件系统总大小  M
					disk.setTotal(usage.getTotal()/1024l);
					// 文件系统剩余大小
					disk.setFree(usage.getFree()/1024l);
					// 文件系统可用大小
					disk.setAvail(usage.getAvail()/1024l);
					// 文件系统已经使用量
					disk.setUsed(usage.getUsed()/1024l);
					list.add(disk);
					break;
				case 3:// TYPE_NETWORK ：网络
					break;
				case 4:// TYPE_RAM_DISK ：闪存
					break;
				case 5:// TYPE_CDROM ：光驱
					break;
				case 6:// TYPE_SWAP ：页面交换
					break;
			}
		}
		return list;
	}
	/**
	 * 获取内存信息
	 * @param sigar
	 * @return
	 * @throws SigarException
	 */
	public static MemoryInfo getMemoryInfo(Sigar sigar) throws SigarException{
		Runtime run = Runtime.getRuntime();
		long max = run.maxMemory();
		long total = run.totalMemory();
		long free = run.freeMemory();
		long maxFree = max - total + free;
		MemoryInfo memoryInfo = new MemoryInfo();
		Mem mem = sigar.getMem();
		memoryInfo.setJvmMax(max/1024l/1024l);
		memoryInfo.setJvmTotal(total/1024l/1024l);
		memoryInfo.setJvmFree(free/1024l/1024l);
		memoryInfo.setJvmMaxFree(maxFree/1024l/1024l);
		memoryInfo.setTotal(mem.getTotal()/1024l/1024l);
		memoryInfo.setUsed(mem.getUsed()/1024l/1024l);
		memoryInfo.setFree(mem.getFree()/1024l/1024l);
		Swap swap = sigar.getSwap();
		memoryInfo.setSwapTotal(swap.getTotal()/1024l/1024l);
		memoryInfo.setSwapUsed(swap.getUsed()/1024l/1024l);
		memoryInfo.setSwapFree(swap.getFree()/1024l/1024l);
		return memoryInfo;
	}
	/**
	 * 获取操作系统信息
	 */
	public static SystemInfo getSystemInfo(){
		SystemInfo info = new SystemInfo();
		Sigar sigar = new Sigar();
		String hostname = "";  
		try {  
		    hostname = InetAddress.getLocalHost().getHostName();  
		} catch (Exception exc) {  
		    try {  
		        hostname = sigar.getNetInfo().getHostName();  
		    } catch (SigarException e) {  
		        hostname = "localhost.unknown";  
		    }  
		}  
		info.setHostName(hostname);  
		// 取当前操作系统的信息  
		OperatingSystem OS = OperatingSystem.getInstance();  
		info.setArch(OS.getArch());
		info.setName(OS.getName());
		// 操作系统的版本号  
		info.setVersion(OS.getVersion());
		return info;
	}
}
